#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "interface.c"

#include <time.h>       /* time */

/*
	We are to write this tester in a way that we can recreate the game later. For this
	we will want to be able to print exactly what each player has in their three groups of
	cards. 

	More specifically we will want to be able to look at each players hand, deck, discard piles. 

*/

// the start of our turn taking chain
int turns(struct gameState 	*state)
{
	int i = 0;
	int random = 0;
	int problems = 0;
	int temp;
	int numActionCards = 0;
	printHand(whoseTurn(state),state);
	printDeck(whoseTurn(state),state);
	printDiscard(whoseTurn(state),	state);
	printf("Number of Actions %d\n", state->numActions );
	printf("Number of Coins %d\n", state->coins );
	printf("Number of Buys %d\n", state->numBuys );

	// get number of actioncards
	for(i = 0; i < state->handCount[whoseTurn(state)]; i++)
	{
		if(state->hand[whoseTurn(state)][i] > gold)
		{
			numActionCards++;
		}
	}
	// do actions
	while(state->numActions!=0 && numActionCards != 0)
	{
		printf("We have %d actions and %d actionCards remaining. \n", state->numActions, numActionCards );
		for(i = 0; i < state->handCount[whoseTurn(state)]; i++)
		{
			if(state->hand[i] > gold && state->numActions != 0)
			{
				playCard(i,1,2,3, &state);
			}
		}

	}
	// buy stuff
	while(state->numBuys > 0 &&  state->coins > 0)
	{
		printf("We have %d Buys and %d coins remaining. \n", state->numBuys, state->coins );
		random = rand() % 10;
		temp = buyCard(random, &state);
		if(temp != -1)
		{
			break;
		}
		printf("There was a problem for player %d trying to buy card %d\n", whoseTurn(state), random );

	}

	// action and buy stage finished. End turn plox
	printf("Player %d has ended their turn\n", whoseTurn(state) );
	endTurn(&state);
}


// print important information to replicate the game
//    **use this to ensure drawing and playing are working correctly. 




int main(int argc, char *argv[])
{
	srand(time(NULL));
	int ranNumPlayers = rand() % 3 + 2;
	int worldSeed = rand() % 100000000;
	int kingdomCards[10];
	int i = 0;
	int counter = 0;
	int problems = 0;
	struct gameState state;
	int game;
	int temp;
// fill kingdomCards with random unique values from 0-10
	printf("We are currently finding random kingdomCards!\n");
	
	while(1)
	{	

		temp = rand() % 10 + 1;
		for(i = 0; i < 10; i++)
		{
			if(kingdomCards[i] == temp)
			{
				problems = 1;
			}
		}
		if(problems != 1)
		{	
			kingdomCards[counter] = temp;
			counter++;

		}
		problems = 0;
		if(counter > 8)
		{
			break;
		}
		//printf("Counter is %d still not breaking\n",counter );
	}

	game = initializeGame(ranNumPlayers, kingdomCards, worldSeed, &state);
	if(game != 0)
	{
		printf("Something went wrong when trying to initialize the game! Try again!!\n");
	} 

	printf("%d player game!\n", ranNumPlayers );
	printf("Don't be a Steph!! Good luck and have FUN! \n");
	
	// Game starts

	printf("Information you will need to know. \n");
	printf("Our random kingdomCards are......\n");
	
	// might need to know kingdomCards
	for(i = 0; i < 10; i++)
	{
		printf("%d ", kingdomCards[i]);
	}
	printf("\n");

	printf("We are about to move into the playing stage. HF!\n");
	while(isGameOver(&state) != 1)
	{
		turns(&state);
	}
	printf("The game has finished. Have a good day!\n");
}